<?php

class Admin_adminController extends Admin_baseController {
    
    public function __construct() {
        parent::__construct();
    }

    public function listAction() {
        $page = Request::get('page', 1);
        $page_size = Request::get('page_size', 15);
        $where = $value = $url = array();
        $name = Request::get('name');
        $role_id = Request::get('role_id');
        $status = Request::get('status');
        if($name) {
            $where[] = 'name like ?';
            $value[] = '%'.$name.'%';
            $url['name'] = $name;
        }
        if ($role_id) {
            $where [] = 'role_id=?';
            $value [] = $role_id;
            $url ['role_id'] = $role_id;
        }
        if ($status) {
            $where [] = 'status=?';
            $value [] = $status;
            $url ['status'] = $status;
        }
        $total = self::model('admin')->count('admin_id', $where, $value);
        $this->assign(array(
            'page' => $page,
            'page_size' => $page_size,
            'role_id' => $role_id,
            'status' => $status,
            'name' => $name,
            'status_list' => self::model('admin')->getAllStatus(),
            'role_list' => self::model('admin_role')->getList(),
            'total' => $total,
            'list' => self::model('admin')->limit(($page - 1) * $page_size, $page_size)->order('admin_id desc')->getAll($where, $value),
            'pageHtml' => self::instance('Pagination')->page($page)->num($page_size)->total($total)->url('/admin_admin/list?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml()
        ));
        $this->display();
    }

    public function editAction() {
        $admin_id = (int) Request::getParams('admin_id');
        $edit_admin_info = array();
        if ($admin_id && !$edit_admin_info = self::model('admin')->getOne('admin_id=?', array($admin_id))) {
            Response::showMsg('管理员信息不存在，修改失败');
        }
        if (Request::isGet()) {
            $this->assign('role_list', self::model('admin_role')->getList());
            $this->assign('status_list', self::model('admin')->getAllStatus());
            $this->assign('adminInfo', $edit_admin_info);
            $this->display();
            return;
        }
        $data = array();
        $data['name'] = Request::getParams('name');
        $data['password'] = Request::getParams('password');
        $data['role_id'] = intval(Request::getParams('role_id'));
        $data['update_time'] = date('Y-m-d H:i:s');
        if(!$data['role_id']) {
            Response::ajax(NORMAL_ERROR, '管理员角色不能为空');
        }
        if (!$data['name']) {
            Response::ajax(NORMAL_ERROR, '管理员账号不能为空');
        }
        if ($data['password']) {
            $result = self::model('admin')->validata($data);
            if($result!==true) {
                Response::ajax(NORMAL_ERROR, $result);
            }
            $data['salt'] = Text::randString(4);
            $data['password'] = md5($data['password'] . $data['salt']);
        } else {
            unset($data['password']);
        }
        if (!$admin_id && self::model('admin')->getOne('name=?', array($data['name']))) {
            Response::ajax(NORMAL_ERROR, '管理员账号已存在,添加失败');
        }
        if ($edit_admin_info && $edit_admin_info['name'] != $data['name'] && self::model('admin')->getOne('name=?', array($data['name']))) {
            Response::ajax(NORMAL_ERROR, '管理员账号已存在,修改失败');
        }
        if (!$admin_id && !$data['password']) {
            Response::ajax(NORMAL_ERROR, '账号密码不能为空');
        }

        if ($admin_id) {
            if (self::model('admin')->update($data, 'admin_id=?', array($admin_id))) {
                $this->addLog('管理员' . self::$admin_info['name'] . '修改管理员:' . json_encode($data));
                Response::ajax(200, '更新管理员信息成功');
            }
        } else {
            $data['add_time'] = date('Y-m-d H:i:s');
            if (self::model('admin')->insert($data)) {
                $this->addLog('管理员' . self::$admin_info['name'] . '添加管理员:' . json_encode($data));
                Response::ajax(200, '添加管理员信息成功');
            }
        }
        Response::ajax(NORMAL_ERROR, '操作失败，请稍后再试');
    }

    public function statusAction() {
        $admin_id = intval(Request::getParams('admin_id'));
        $status = intval(Request::getParams('status'));
        if (self::model('admin')->update(['status' => $status], 'admin_id=?', $admin_id)) {
            Response::ajax(200, '修改成功');
        }
        Response::ajax(NORMAL_ERROR, '修改失败,请稍后再试');
    }

    public function role_statusAction() {
        $role_id = intval(Request::getParams('role_id'));
        $status = intval(Request::getParams('status'));
        if (self::model('admin_role')->delete('role_id=?', $role_id)) {
            Response::ajax(200, '删除成功');
        }
        Response::ajax(NORMAL_ERROR, '修改失败,请稍后再试');
    }

    public function roleAction() {
        $page = Request::get('page', 1);
        $page_size = Request::get('page_size', 15);
        $where = $value = $url = array();

        $total = self::model('admin_role')->count('role_id', $where, $value);
        $this->assign(array(
            'page' => $page,
            'page_size' => $page_size,
            'total' => $total,
            'admin_group_list' => self::model('admin')->groupByRole(),
            'list' => self::model('admin_role')->limit(($page - 1) * $page_size, $page_size)->order('sort desc')->getAll($where, $value),
            'pageHtml' => self::instance('Pagination')->page($page)->num($page_size)->total($total)->url('/admin_admin/role?' . ($url ? http_build_query($url) : '') . '&page=')->rendHtml()
        ));
        $this->display();
    }

    public function role_editAction() {
        $role_id = (int) Request::getParams('role_id');
        $role_info = array();
        if ($role_id && !$role_info = self::model('admin_role')->getOne('role_id=?', array($role_id))) {
            Response::showMsg('信息不存在，修改失败');
        }
        if (Request::isGet()) {
            $this->assign('node_list', self::model('admin_node')->getList());
            $this->assign('role_node_list', self::model('admin_access')->getAllByRoleId($role_id));
            $this->assign('role_info', $role_info);
            $this->display();
            return;
        }
        $data = array();
        $data['name'] = Request::getParams('name');
        $access_node = Request::getParams('node');
        if (!$data['name']) {
            Response::ajax(NORMAL_ERROR, '角色不能为空');
        }
        if(!Validation::isLength($data['name'], 1, 50)) {
            Response::ajax(NORMAL_ERROR, '角色名称长度不能超过50个字符');
        }
        if(!$access_node) {
            Response::ajax(NORMAL_ERROR, '权限不能为空');
        }
        $check_name_info = self::model('admin_role')->getOne('name=?', array($data['name']));
        if (!$role_id && $check_name_info) {
            Response::ajax(NORMAL_ERROR, '角色已存在,添加失败');
        }
        if ($role_info && $role_info['name'] != $data['name'] && $check_name_info) {
            Response::ajax(NORMAL_ERROR, '角色已存在,修改失败');
        }
        

        if ($role_id) {
            if($role_info['is_hide']) {
                Response::ajax(NORMAL_ERROR, '抱歉,该角色信息不能修改');
            }
            if (self::model('admin_role')->update($data, 'role_id=?', array($role_id))) {
                self::model('admin_access')->updateRole($role_id, $access_node);
                $this->addLog('管理员' . self::$admin_info['name'] . '修改管理员角色:' . serialize($data));
                Response::ajax(200, '更新管理员信息成功');
            }
        } else {
            if ($role_id = self::model('admin_role')->insert($data, true)) {
                self::model('admin_access')->updateRole($role_id, $access_node);
                $this->addLog('管理员' . self::$admin_info['name'] . '添加管理员角色:' . serialize($data));
                Response::ajax(200, '添加管理员信息成功');
            }
        }
        $this->addLog('管理员' . self::$admin_info['name'] . '操作管理员信息失败:' . serialize($data));
        Response::ajax(NORMAL_ERROR, '操作失败，请稍后再试');
    }

    public function nodeAction() {
        $this->assign(array(
            'list' => self::model('admin_node')->getMenuList()
        ));
        $this->display();
    }

    public function node_editAction() {
        $node_id = (int) Request::getParams('node_id');
        $node_info = array();
        if ($node_id && !$node_info = self::model('admin_node')->getOne('id=?', array($node_id))) {
            Response::showMsg('信息不存在，修改失败');
        }
        if (Request::isGet()) {
            $this->assign('node_list', self::model('admin_node')->getMenuList());
            $this->assign('node_info', $node_info);
            $this->display();
            return;
        }
        $data = array();
        $data['name'] = Request::getParams('name');
        $data['sort'] = Request::getParams('sort');
        $data['pid'] = Request::getParams('pid');
        $data['icon'] = $_POST['icon'];
        $data['is_show'] = Request::getParams('is_show');
        $data['url_path'] = Request::getParams('url_path');
        if (!$data['name']) {
            Response::ajax(NORMAL_ERROR, '节点名称不能为空');
        }

        if ($node_id) {
            if (self::model('admin_node')->update($data, 'id=?', array($node_id))) {
                self::model('admin_node')->deleteListCache();
                $this->addLog('管理员' . self::$admin_info['name'] . '修改管理节点:' . serialize($data));
                Response::ajax(200, '更新信息成功');
            }
        } else {
            if (self::model('admin_node')->insert($data)) {
                self::model('admin_node')->deleteListCache();
                $this->addLog('管理员' . self::$admin_info['name'] . '添加管理节点:' . serialize($data));
                Response::ajax(200, '添加信息成功');
            }
        }
        $this->addLog('管理员' . self::$admin_info['name'] . '操作管理节点信息失败:' . serialize($data));
        Response::ajax(NORMAL_ERROR, '操作失败，请稍后再试');
    }

    public function node_statusAction() {
        $node_id = intval(Request::getParams('node_id'));
        $status = intval(Request::getParams('status'));
        if (self::model('admin_node')->delete('id=?', $node_id)) {
            self::model('admin_node')->deleteListCache();
            Response::ajax(200, '删除成功');
        }
        Response::ajax(NORMAL_ERROR, '修改失败,请稍后再试');
    }

    public function logAction() {
        $page = Request::get('page', 1);
        $page_size = Request::get('page_size', 15);
        $where = $value = $url = array();

        $total = self::model('admin_log')->count('id', $where, $value);
        $this->assign(array(
            'page' => $page,
            'page_size' => $page_size,
            'total' => $total,
            'list' => self::model('admin_log')->getList()
        ));
        $this->display();
    }

}
